﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing                 Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: Task.cs
 * --------------------------------------------------------------------------
 * Copyright 2009 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System;

namespace Hyldahl.Services.TaskProcessing
{
    [Serializable]
    public class Task : System.IComparable<Task>
    {
        /*****************************************************
         * FIELDS
         *****************************************************/
        private Guid        id;
        private string      taskType;
        private string      data;
        private string      resultData;
        private bool        isProcessed;
        private bool?       completedSuccessfully;
        private long?       processingTime;
        private DateTime    createdDate             = DateTime.MinValue;
        private DateTime?   readDate;
        private DateTime?   completedDate;

        /*****************************************************
         * METHODS
         *****************************************************/

        #region IComparable<Task> Members

        /// <summary>
        /// Compares the current object with another object of the same type.
        /// </summary>
        /// <param name="other">An object to compare with this object.</param>
        /// <returns>
        /// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the <paramref name="other"/> parameter.Zero This object is equal to <paramref name="other"/>. Greater than zero This object is greater than <paramref name="other"/>.
        /// </returns>
        public int CompareTo(Task other)
        {
            if (other == null)
                throw new ArgumentNullException("other");

            int compare = 0;

            compare = this.Id.CompareTo(other.Id);
            if (compare != 0)
                return compare;

            compare = this.TaskType.CompareTo(other.TaskType);
            if (compare != 0)
                return compare;

            compare = this.Data.CompareTo(other.Data);
            if (compare != 0)
                return compare;

            if (this.ResultData != null && other.ResultData == null)
                return 1;

            if (this.ResultData == null && other.ResultData != null)
                return -1;

            if (this.ResultData == null && other.ResultData == null)
                compare = 0;
            else
            {
                compare = this.ResultData.CompareTo(other.ResultData);
                if (compare != 0)
                    return compare;
            }

            compare = this.IsProcessed.CompareTo(other.IsProcessed);
            if (compare != 0)
                return compare;

            compare = this.CreatedDate.CompareTo(other.CreatedDate);
            if (compare != 0)
                return compare;

            compare = Nullable.Compare<bool>(this.CompletedSuccessfully, other.CompletedSuccessfully);
            if (compare != 0)
                return compare;

            compare = Nullable.Compare<long>(this.ProcessingTime, other.ProcessingTime);
            if (compare != 0)
                return compare;
            
            compare = Nullable.Compare<DateTime>(this.ReadDate, other.ReadDate);
            if (compare != 0)
                return compare;

            compare = Nullable.Compare<DateTime>(this.CompletedDate, other.CompletedDate);
            if (compare != 0)
                return compare;

            return compare;
        }

        #endregion

        /*****************************************************
         * PROPERTIES
         *****************************************************/

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public Guid Id
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// Gets or sets the type of the task.
        /// </summary>
        /// <value>The type of the task.</value>
        public string TaskType
        {
            get { return taskType; }
            set { taskType = value; }
        }

        /// <summary>
        /// Gets or sets the data.
        /// </summary>
        /// <value>The data.</value>
        public string Data
        {
            get { return data; }
            set { data = value; }
        }

        /// <summary>
        /// Gets or sets the result data.
        /// </summary>
        /// <value>The result data.</value>
        public string ResultData
        {
            get { return resultData; }
            set { resultData = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is processed.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is processed; otherwise, <c>false</c>.
        /// </value>
        public bool IsProcessed
        {
            get { return isProcessed; }
            set { isProcessed = value; }
        }

        /// <summary>
        /// Gets or sets the completed successfully.
        /// </summary>
        /// <value>The completed successfully.</value>
        public bool? CompletedSuccessfully
        {
            get { return completedSuccessfully; }
            set { completedSuccessfully = value; }
        }

        /// <summary>
        /// Gets or sets the processing time.
        /// </summary>
        /// <value>The processing time.</value>
        public long? ProcessingTime
        {
            get { return processingTime; }
            set { processingTime = value; }
        }

        /// <summary>
        /// Gets or sets the created date.
        /// </summary>
        /// <value>The created date.</value>
        public DateTime CreatedDate
        {
            get { return createdDate; }
            set { createdDate = value; }
        }

        /// <summary>
        /// Gets or sets the read date.
        /// </summary>
        /// <value>The read date.</value>
        public DateTime? ReadDate
        {
            get { return readDate; }
            set { readDate = value; }
        }

        /// <summary>
        /// Gets or sets the completed date.
        /// </summary>
        /// <value>The completed date.</value>
        public DateTime? CompletedDate
        {
            get { return completedDate; }
            set { completedDate = value; }
        }
    }
}
